'/
' ProjectName
' 
' =========================================================================
' 
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
' 
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
' 
' You should have received a copy of the GNU General Public License
' along with this program.  If not, see <http://www.gnu.org/licenses/>.
'/

include "Sound.bmx"
include "SoundQue.bmx"

type SoundManager extends Base

	'// -------------------------------------------------------------------------------------------------------------
	'// Initialize the sound manager.
	'// -------------------------------------------------------------------------------------------------------------
	method init()
	
		sConsoleManager.writeLog("Initializing OpenAL.");
		enableOpenALAudio();
		setAudioDriver("OpenAL");
		
		sConsoleManager.writeLog("Creating sound ques.");
		addQue("menu_fx");
		addQue("player_fx");
		addQue("env_fx");
		addQue("menu_music");
		addQue("ingame_music");
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Deinitialize the sound manager.
	'// -------------------------------------------------------------------------------------------------------------
	method deinit()
	
		for local que:SoundQue = eachin m_queList
		
			if (que.m_curSoundPlaying) ..
				que.m_curSoundPlaying.stop();
		
		next
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Update all ques. When this method is called, the next sound in each que gets played, and removed from the
	'// que.
	'// -------------------------------------------------------------------------------------------------------------
	method update()
	
		for local que:SoundQue = eachin m_queList
		
			que.update();
		
		next
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Load a sound to memory.
	'// -------------------------------------------------------------------------------------------------------------
	method loadSound(name:string, url:string)
	
		sConsoleManager.writeLog("... " + url);
		m_soundMap.insert(name, Sound.create(url));
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Add a que to the list of ques.
	'// -------------------------------------------------------------------------------------------------------------
	method addQue(name:string)
	
		local que:SoundQue = SoundQue.create(name);
		m_queList.addLast(que);
		m_queMap.insert(name, que);
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Return a specific que.
	'// -------------------------------------------------------------------------------------------------------------
	method getQue:SoundQue(name:string)
	
		return SoundQue(m_queMap.valueForKey(name));
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Return a specific que.
	'// -------------------------------------------------------------------------------------------------------------
	method getSound:Sound(name:string)
	
		return Sound(m_soundMap.valueForKey(name));
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Play a sound.
	'// -------------------------------------------------------------------------------------------------------------
	method playSound(que:string, name:string, distance:double = -1.0)
	
		SoundQue(m_queMap.valueForKey(que)).addSound(Sound(m_soundMap.valueForKey(name)), distance);
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Containers for sounds that are loaded to memory.
	'// -------------------------------------------------------------------------------------------------------------
	field m_soundMap:TMap = new TMap;
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Containers for ques.
	'// -------------------------------------------------------------------------------------------------------------
	field m_queList:TList = new TList;
	field m_queMap:TMap = new TMap;

end type

global sSoundManager:SoundManager = new SoundManager;
